package org.budo.mybatis.criteria.search.service.impl;

import java.util.List;
import java.util.Map;

import org.budo.mybatis.criteria.query.CriteriaQuery;
import org.budo.mybatis.criteria.query.ParsedCriteriaQuery;
import org.budo.mybatis.criteria.query.parser.CriteriaQueryParser;
import org.budo.mybatis.dao.MybatisDao;
import org.budo.support.assertion.Assert;
import org.budo.support.dao.page.Page;
import org.budo.support.dao.page.PageModel;

/**
 * @author li
 */
public class CriteriaSearchServiceImpl extends AbstractCriteriaSearchService {
    @Override
    public <T> PageModel<T> criteriaSearch(Class<T> entityType, String[] fields, Map<String, Object> filter, Map<String, Object> parameters, Page page) {
        Assert.notNull(entityType, "entityType can not be null");
        Assert.notNull(fields, "fields can not be null");
        Assert.notNull(filter, "filter can not be null");
        Assert.notNull(parameters, "parameters can not be null");
        Assert.notNull(page, "page can not be null");

        CriteriaQueryParser criteriaQueryParser = this.getCriteriaQueryParser();
        Assert.notNull(criteriaQueryParser, "criteriaQueryParser can not be null");

        MybatisDao mybatisDao = this.getMybatisDao();
        Assert.notNull(mybatisDao, "mybatisDao can not be null");

        CriteriaQuery criteriaQuery = new CriteriaQuery(entityType, fields, filter);
        ParsedCriteriaQuery parsedCriteriaQuery = criteriaQueryParser.parseCriteriaQuery(criteriaQuery);

        List<T> list = mybatisDao.listBySql(entityType, parsedCriteriaQuery.getSql(), parameters, page);

        // TODO 对多关联

        return new PageModel<T>(list, 999);
    }
}